import { SharedMethodService } from './../../../../@core/services/Sharedmethod/shared-method.service';
import { Component, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { ReviewRolesService } from 'src/app/@core/services/ReviewRoles/review-roles.service';
import { AddReviewFlowComponent } from './add-review-flow/add-review-flow.component';
import { EditReviewFlowComponent } from './edit-review-flow/edit-review-flow.component';
import { Router } from '@angular/router';
@Component({
  selector: 'app-review-flow',
  templateUrl: './review-flow.component.html',
  styleUrls: ['./review-flow.component.scss'],
})
export class ReviewFlowComponent {
  ReviewFlowData: any[] = [];
  allCheckedStatus = false;
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20,
  };
  checkedIds = [];
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any }[];
  config = {
    id: 'Add-role',
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }; //弹窗配置
  filterData: string = '';
  dataTableOptions = {
    columns: [
      {
        field: 'type',
        header: '类型',
        fieldType: 'text',
      },
      {
        field: '描述',
        header: '描述',
        fieldType: 'boolean',
      },
      {
        field: '状态',
        header: '状态',
        fieldType: 'boolean',
      },
    ],
  }; //表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px',
    },
    {
      field: '操作',
      width: '',
    },
    {
      field: 'name',
      width: '',
    },
    {
      field: 'isEmpowerment',
      width: '',
    },
    {
      field: 'isEmpowerment',
      width: '120px',
    },
  ]; //表格宽度配置
  @ViewChild(DataTableComponent, { static: true })
  datatable: DataTableComponent;
  constructor(
    private dialogService: DialogService,
    private ReviewRolesService: ReviewRolesService,
    private Router: Router,
    private SharedMethodService: SharedMethodService
  ) {}
  ngOnInit(): void {
    this.ReviewRolesService.GetApprovalFlowConfigs(
      this.pager.pageIndex,
      this.pager.pageSize
    ).subscribe((data: any) => {
      this.pager.total = data.totalCount;
      this.ReviewFlowData = data.items;
    });
  }
  pageIndexChange($event: any) {
    this.pager.pageIndex = $event;
    this.ReviewRolesService.GetApprovalFlowConfigs(
      this.pager.pageIndex,
      this.pager.pageSize
    ).subscribe((data: any) => {
      this.pager.total = data.totalCount;
      this.ReviewFlowData = data.items;
    });
  } //切换页码
  pageSizeChange($event: any) {
    this.ReviewRolesService.GetApprovalFlowConfigs(
      this.pager.pageIndex,
      this.pager.pageSize
    ).subscribe((data: any) => {
      this.pager.total = data.totalCount;
      this.ReviewFlowData = data.items;
    });
  } //改变每页显示数量
  CheckDelete(dialogtype?: string) {
    if (this.checkedIds.length > 0) {
      const results = this.dialogService.open({
        ...this.config,
        title: '删除',
        content: '确认删除该选中数据？',
        dialogtype: dialogtype,
        onClose: () => {},
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              this.ReviewRolesService.DeleteApprovalFlowConfigRange(
                this.checkedIds
              ).subscribe(
                (data) => {
                  results.modalInstance.hide();
                  this.showToast('success', '成功', '该审核流配置删除成功');
                  this.checkedIds = [];
                  this.ReviewRolesService.GetApprovalFlowConfigs(
                    this.pager.pageIndex,
                    this.pager.pageSize
                  ).subscribe((data: any) => {
                    this.pager.total = data.totalCount;
                    this.ReviewFlowData = data.items;
                  });
                },
                (error: any) => {
                  results.modalInstance.hide();
                  if (error.status == 404) {
                    this.showToast('error', '失败', '服务器连接失败');
                  } else {
                    this.showToast('error', '失败', error.error);
                  }
                }
              );
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });
    } else {
      this.showToast('warn', '警告', '请先选择要删除的数据');
    }
  } //删除
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  } //提示框
  Creat(dialogtype?: string) {
    let data = {
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      placement: 'center',
      offsetY: '-100px',
      data: data,
      title: '添加审核流程',
      content: AddReviewFlowComponent,
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if (results.modalContentInstance.PostReviewFlow.approvalType) {
              this.ReviewRolesService.CreateApprovalFlow(
                results.modalContentInstance.PostReviewFlow
              ).subscribe(
                (data: any) => {
                  this.showToast('success', '成功', '审核流配置添加成功');
                  results.modalInstance.hide();
                  this.ReviewRolesService.GetApprovalFlowConfigs(
                    this.pager.pageIndex,
                    this.pager.pageSize
                  ).subscribe((data: any) => {
                    this.pager.total = data.totalCount;
                    this.ReviewFlowData = data.items;
                  });
                },
                (error: any) => {
                  results.modalInstance.hide();
                  this.showToast('error', '失败', error.error);
                }
              );
              results.modalInstance.hide();
            } else if (
              !results.modalContentInstance.PostReviewFlow.approvalType
            ) {
              data.display = true;
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  } //创建审核流配置
  update(rowItem: any, dialogtype?: string) {
    let data = {
      display: false,
      rowItem: rowItem,
    };
    const results = this.dialogService.open({
      ...this.config,
      placement: 'center',
      offsetY: '-100px',
      data: data,
      title: '更新审核流程',
      content: EditReviewFlowComponent,
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if (results.modalContentInstance.PutReviewFlow.approvalType) {
              this.ReviewRolesService.UpdateApprovalFlowConfig(
                rowItem.approvalFlowConfigId,
                results.modalContentInstance.PutReviewFlow
              ).subscribe(
                (data: any) => {
                  this.showToast('success', '成功', '审核流配置编辑成功');
                  results.modalInstance.hide();
                  this.ReviewRolesService.GetApprovalFlowConfigs(
                    this.pager.pageIndex,
                    this.pager.pageSize
                  ).subscribe((data: any) => {
                    this.pager.total = data.totalCount;
                    this.ReviewFlowData = data.items;
                  });
                },
                (error: any) => {
                  results.modalInstance.hide();
                  this.showToast('error', '失败', error.error);
                }
              );

              results.modalInstance.hide();
            }
            data.display = true;
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  } //创建审核流配置
  FlowNode(rowItem: any) {
    this.Router.navigate([
      '/pages/systemset/Review-Flow-Node',
      rowItem.approvalFlowConfigId,
    ]);
  }
  onRowCheckChange(
    checked: any,
    rowIndex: any,
    nestedIndex: any,
    rowItem: { $checked: any; $halfChecked: boolean }
  ) {
    this.SharedMethodService.onRowCheckChange(
      checked,
      rowIndex,
      nestedIndex,
      rowItem,
      this.datatable
    );
  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(
      this.datatable,
      'approvalFlowConfigId'
    ).subscribe((data: any) => {
      this.checkedIds = data;
    });
  } //获取选中行
  cleared() {
    this.SharedMethodService.cleared(
      this.checkedIds,
      this.ReviewFlowData,
      this.datatable
    ).subscribe((data: any) => {
      this.checkedIds = data;
    });
  } //清空选中行
  Enable(id: any) {
    this.ReviewRolesService.ChangeApprovalFlowConfig(id, true).subscribe(
      (data: any) => {
        this.showToast('success', '成功', '审核流配置启用成功');
        this.ReviewRolesService.GetApprovalFlowConfigs(
          this.pager.pageIndex,
          this.pager.pageSize
        ).subscribe((data: any) => {
          this.pager.total = data.totalCount;
          this.ReviewFlowData = data.items;
        });
      }
    );
  } //启用
  Disable(id: any) {
    this.ReviewRolesService.ChangeApprovalFlowConfig(id, false).subscribe(
      (data: any) => {
        this.showToast('success', '成功', '审核流配置启用成功');
        this.ReviewRolesService.GetApprovalFlowConfigs(
          this.pager.pageIndex,
          this.pager.pageSize
        ).subscribe((data: any) => {
          this.pager.total = data.totalCount;
          this.ReviewFlowData = data.items;
        });
      }
    );
  } //禁用
}
